<?php
namespace Admin\Controller;

class NoticeController extends AdminController {
    
    public function index() {
        $list = $this->lists('notice');
        
        $this->assign('_list', $list);
        $this->meta_title = "官方公告";
        $this->display();
    }
    
    public function add() {
        if (IS_POST) {
            $data = I();
            
            empty($data['title']) && $this->error('标题不能为空');
            (mb_strlen($data['title']) < 4 || mb_strlen($data['title']) > 100) && $this->error('标题长度不能小于4或大于100字符');
            
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
            $id = M('notice')->add($data);
            $id > 0 ? $this->success('操作成功', U('index')) : $this->error('操作失败，未知错误');
        }
        
        $this->meta_title = "新增公告";
        $this->display('edit');
    }
    
    public function edit() {
        $id = I('get.id', 0);
        $info = M('notice')->find($id);
        $info || $this->error('参数有误');
        
        if (IS_POST) {
            $data = I();
            
            empty($data['title']) && $this->error('标题不能为空');
            (mb_strlen($data['title']) < 4 || mb_strlen($data['title']) > 100) && $this->error('标题长度不能小于4或大于100字符');
            
            $data['update_time'] = NOW_TIME;
            $status = M('notice')->where(array('id'=>$id))->save($data);
            $status !== false ? $this->success('操作成功', U('index')) : $this->error('操作失败，未知错误');
        }
        
        $this->assign('info', $info);
        $this->meta_title = '编辑公告';
        $this->display('edit');
    }
    
    public function delete() {
        $id = I('get.id', 0);
        $info = M('notice')->find($id);
        $info || $this->error('参数有误');
        $status = M('notice')->where(array('id'=>$id))->delete();
        $status !== false ? $this->success('操作成功') : $this->error('未知错误');
    }

    /**
     * 每日一笑列表
     */
    public function  everysmile(){
        $list = $this->lists('smile');

        $this->assign('_list', $list);
        $this->meta_title = "官方公告";
        $this->display();
    }

    public function editsmile() {
        $id = I('get.id', 0);
        $info = M('smile')->find($id);
        $info || $this->error('参数有误');

        if (IS_POST) {
            $data = I();

            empty($data['title']) && $this->error('标题不能为空');
            //(mb_strlen($data['title']) < 4 || mb_strlen($data['title']) > 100) && $this->error('标题长度不能小于4或大于100字符');

            $data['update_time'] = NOW_TIME;
            $status = M('smile')->where(array('id'=>$id))->save($data);
            $status !== false ? $this->success('操作成功', U('everysmile')) : $this->error('操作失败，未知错误');
        }

        $this->assign('info', $info);
        $this->meta_title = '编辑';
        $this->display();
    }



}